<template>
	<div>
		<!-- 导入弹窗 -->
    <el-dialog 
      title="风险点辨识导入" 
      append-to-body 
      :visible.sync="excelBox" 
      :close-on-click-modal="false" 
      :before-close="handleCloseExcelBox"
      width="555px"
    >
      <avue-form 
        :option="excelOption" 
        v-model="excelForm" 
        :upload-after="uploadAfter" 
        :upload-before="beforeUpload"
        :upload-error="uploadError"
      >
        <template slot="excelTemplate">
          <el-button type="primary" @click="handleTemplate">
            点击下载<i class="el-icon-download el-icon--right"></i>
          </el-button>
        </template>
      </avue-form>
    </el-dialog>
	</div>
</template>

<script>
export default {
  props: {
    excelBox: {default: false}
  },
  data() {
    return {
      excelForm: {},
      uploadFailed: false,
      excelOption: {
        submitBtn: false,
        emptyBtn: false,
        column: [{
            label: '模板上传',
            prop: 'excelFile',
            type: 'upload',
            drag: true,
            loadText: '模板上传中，请稍等',
            showFileList: false,
            span: 24,
            propsHttp: {
              fileName: 'multipartFile',
              res: 'data'
            },
            tip: '请上传 .xls,.xlsx 标准格式文件',
            action: "/api/riskPoint/riskmanagement/import",
            headers: {}
          },
          {
            label: '模板下载',
            prop: 'excelTemplate',
            formslot: true,
            span: 24,
          }
        ]
      },
    }
  },
  methods: {

    // 导入前判断
    beforeUpload(file, done, loading) {
      this.uploadFailed = false;
      var testmsg = file.name.substring(file.name.lastIndexOf('.') + 1)
      const extension = testmsg === 'xls'
      const extension2 = testmsg === 'xlsx'
      if(!extension && !extension2) {
        this.$message.warning('请上传 .xls,.xlsx 标准格式文件!');
        loading()
      } else {
        done()
      }
    },

    // 模板下载
    handleTemplate() {
      let str = this.$UploadUrl + '/hg/file/template/riskPointTemplate.xlsx';
      window.open(str);
    },

    // 导入失败后
    uploadError(error, column) {
      console.log(error,column);
      this.uploadFailed = true;
    },

    // 导入成功后
    uploadAfter(res, done) {
      done();
      if(this.uploadFailed === true) {
        return;
      }
      this.$emit('uploadAfter');
    },

    // 关闭弹窗
    handleCloseExcelBox(done) {
      done();
      this.$emit('handleCloseExcelBox');
    }

  }
}
</script>